package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ysd.dao.impl.AdminDaoimpl;
import com.ysd.entity.Admin;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminServlet extends BaseServlet {
	/**
	 * localhost:8080/xiyangyangchaoshi/Admin?reqName=getAdminByname
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAdminByname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		AdminDaoimpl aDao=new AdminDaoimpl();
		Admin admin=aDao.getAdminByname(username);
		
		if(admin!=null) {
			if(admin.getPassword().equals(password)) {
				HttpSession session= request.getSession();
				session.setAttribute("admin", admin);
				out.print(Result.toClient(true, admin, "登录成功"));
			}else {
				out.print(Result.toClient(false, "", "密码错误"));
			}
		}else {
			out.print(Result.toClient(false, "", "账号错误"));
		}
		out.flush();
		out.close();
	}

}
